#include "Stack.h"

void STInit(ST& st, int n)
{
	st.arr = (STDatatype*)malloc(sizeof(STDatatype) * n);
	st.capacity = n;
	st.top = 0;
}

void STPush(ST& st, STDatatype x)
{
	if (st.capacity == st.top)
	{
		int newcapacity = st.capacity == 0 ? sizeof(STDatatype) : 2 * st.capacity;
		STDatatype* ret = (STDatatype*)realloc(st.arr, sizeof(STDatatype) * newcapacity);
		if (ret == NULL)
		{
			perror("realloc");
			return;
		}
		st.capacity = newcapacity;
		st.arr = ret;
	}
	st.arr[st.top] = x;
	st.top++;
}

//STDatatype& STTop(ST& st)
//{
//	return st.arr[st.top-1];
//}

int& STTop(ST& rs)
{
	//assert(rs.top > 0);
	return rs.arr[rs.top-1];
}

void STDstory(ST& st)
{
	if (st.arr)
	{
		free(st.arr);
		st.arr = nullptr;
	}
	st.capacity = st.top = 0;
}


